﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZhouYu.DreamTour.BLL.IService;
using ZhouYu.DreamTour.DAL.IRepository;
using ZhouYu.DreamTour.Model;
using ZhouYu.DreamTour.Romote;
using ZhouYu.DreamTour.ViewModel;

namespace ZhouYu.DreamTour.BLL.Service
{
    public class SmsInfoService:ISmsInfoService
    {
        private ISmsInfoRepository _smsInfoRepository;
        public SmsInfoService(ISmsInfoRepository smsInfoRepository)
        {
            _smsInfoRepository = smsInfoRepository;
        }

        /// <summary>
        /// 添加短信发送记录
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public ResponseResult AddSmsInfo(string tel)
        {
            //创建响应结果类对象
            ResponseResult responseResult = new ResponseResult();
            //创建腾讯云对象
            TenXunYunSMS tenXunYunSMS = new TenXunYunSMS();
            try
            {
                //获取配置信息
                tenXunYunSMS.appId = Convert.ToInt32(ConfigurationManager.AppSettings["appId"]);
                tenXunYunSMS.appKey = ConfigurationManager.AppSettings["appKey"];
                tenXunYunSMS.SetSMS(tel);
            }
            catch (Exception ex)
            {
                responseResult.Success = false;
                return responseResult;
            }
            //创建短信类对象
            SmsInfo smsInfo = new SmsInfo();
            //赋值
            smsInfo.Code = tenXunYunSMS.Code;
            smsInfo.TelPhone = Convert.ToInt64(tel);
            //调用添加短信发送记录
            responseResult.Success = _smsInfoRepository.AddSmsInfo(smsInfo)>0;         
            return responseResult;
        }


        /// <summary>
        /// 查询验证码是否存在
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public ResponseResult CheckCode(SmsInfo smsInfo)
        {
            //创建响应结果类对象
            ResponseResult responseResult = new ResponseResult();
            //调用查询验证码是否存在
            responseResult.Success= _smsInfoRepository.CheckCode(smsInfo)>0;          
            return responseResult;
        }


        /// <summary>
        /// 根据手机号查询用户是否存在
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public ResponseResult IFExistUserByPhone(long phone)
        {
            //创建响应结果类对象
            ResponseResult responseResult = new ResponseResult();
            //调用根据手机号查询用户是否存在
            responseResult.data = _smsInfoRepository.IFExistUserByPhone(phone);
            return responseResult;
        }


        /// <summary>
        /// 如果手机号未注册便将手机号存入数据库
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public ResponseResult AddPhoneIntoUser(long phone)
        {
            //创建响应结果类对象
            ResponseResult responseResult = new ResponseResult();
            //调用如果手机号未注册便将手机号存入数据库
            responseResult.Success= _smsInfoRepository.AddPhoneIntoUser(phone)>0;
            return responseResult;
        }



        /// <summary>
        /// 发送酒店预定成功短信
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public ResponseResult SendHotelSms(string phone, string userName, string hotelName)
        {
            //创建响应结果类对象
            ResponseResult responseResult = new ResponseResult();
            //创建酒店预定短信对象
            TenXunYunSMSForHotelOrder tenXunYunSMSForHotelOrder = new TenXunYunSMSForHotelOrder();
            try
            {
                //获取配置信息
                tenXunYunSMSForHotelOrder.appId = Convert.ToInt32(ConfigurationManager.AppSettings["appId"]);
                tenXunYunSMSForHotelOrder.appKey = ConfigurationManager.AppSettings["appKey"];
                //调用发短信方法
                tenXunYunSMSForHotelOrder.SetSMS(phone, userName, hotelName);
                //未报异常则返回true
                responseResult.Success = true;
            }
            catch (Exception ex)
            {
                //出现异常则返回false
                responseResult.Success = false;
            }
            return responseResult;
        }
    }
}
